package com.moli.iov.alarm.service.stat.impl;

import com.moli.iov.alarm.model.po.StatAlarm;
import com.moli.iov.alarm.repositories.StatAlarmDao;
import com.moli.iov.alarm.service.stat.IAlarmStatService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.ConstactUtil;
import com.moli.iov.util.DateUtil;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class IAlarmStatServiceImpl implements IAlarmStatService {

    @Resource
    StatAlarmDao statAlarmDao;

    public static String getTimeDay(int index) {
//        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
//        TimeZone.setDefault(tz);
        Long l = System.currentTimeMillis()+(index*86400000L);
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String date = fmt.format(new Date(l));
        return date;
    }

    public static void main(String[] args) {
        List<StatAlarm> list = getList();
        for (StatAlarm statAlarm : list) {
            System.out.println(statAlarm.toString());
        }
    }

    public static List<StatAlarm> getList() {
        List<StatAlarm> list = new ArrayList<>();

        for (int i = -1; i >= -60; i--) {
            StatAlarm sa = new StatAlarm();
            sa.setVehicleCount(5000L);
            sa.setCompanyId(1L);
            sa.setDate(getTimeDay(i));
            sa.setThreeLvAlarmCount((long)(Math.random()*(1000-200))+200);
            sa.setOneLvAlarmCount((long)(Math.random()*(1000-200))+200);
            sa.setTwoLvAlarmCount((long)(Math.random()*(1000-200))+200);
            sa.setMileageExceptionChangeCount((long)(Math.random()*(1000-200))+200);
            sa.setOffLineCount((long)(Math.random()*(1000-200))+200);
            sa.setInFenceAlarmCount((long)(Math.random()*(1000-200))+200);
            sa.setOutFenceAlarmCount((long)(Math.random()*(1000-200))+200);
            list.add(sa);
        }
        return list;
    }

    @Override
    public List<StatAlarm> listStatAlarm(Long companyId, Integer statDays) {

//        if(1==1){
//            return getList();
//        }

        if (statDays == null) {
            RestResponse.failure("statDays,统计类型不能为null");
        }

        Criteria criteria = new Criteria();

        if (companyId == null) {
            criteria.and("companyId").is(ConstactUtil.TOTAL_COMPANY_ID);
        } else {
            criteria.and("companyId").is(companyId);
        }

        criteria.and("date")
                .gte(DateUtil.subDay(statDays))
                .lte(DateUtil.subDay(1));

        Query query = new Query(criteria);
        query.with(new Sort(Sort.Direction.DESC, "date"));

        List<StatAlarm> list = statAlarmDao.find(query);
        return list;
    }
}
